<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        h4,
        p {
            margin: 0;
            font-weight: normal;
        }

        a {
            text-decoration: none;
        }

        body {
            background-color: #eee;
        }

        #app {
            background-color: #fff;
            width: 900px;
            margin: 50px auto;
            text-align: center;
            box-shadow: 3px 3px 3px rgba(0, 0, 0, 0.5);
            padding: 1em 2em;
        }

        .list {
            padding: 1em;
            width: 100%;
            box-sizing: border-box;
            display: flex;
            flex-wrap: wrap;
        }

        .book {
            position: relative;
            flex-basis: 22%;
            margin-right: 2%;
            margin-bottom: 1em;
            text-align: center;
            background-color: #fff;
            padding: 1em 0;
            transition: all .2s linear;
        }

        .book:hover {

            box-shadow: 0 15px 30px rgba(0, 0, 0, .1);
            transform: translate3d(0, -2px, 0);

        }

        .title {
            line-height: 2em;
            margin: 0;
            padding: 0;
        }

        .figure img {
            width: 150px;
            height: 150px;
        }

        .desc {
            margin: 0;
            font-size: 16px;
        }

        .price {
            margin: 0;
            font-size: 14px;
        }

        .btn {
            cursor: pointer;
            position: absolute;
            right: 0.8em;
            top: 0.8em;
            border: none;
            color: #ff6700;
            padding: .5em .5em;
            font-size: 12px;
        }

        .btn:active {
            border: none;
            user-select: none;
            outline: 0 none !important;
        }

        .btn:hover {
            background-color: #ff6700;
            color: #fff
        }

        .selected {
            border: 2px solid #ccc;
            box-shadow: 0 15px 30px rgba(0, 0, 0, .3);
        }

        .cart {
            border: 1px solid #eee;
            background-color: #fff;
            text-align: center;
            position: fixed;
            padding: 1em;
            right: 100px;
            top: 200px;
        }
    </style>
</head>

<body>
    <div id="app">
        <h3 title="哈哈哈我是一个h3">我的购物车</h3>
        <div class="list" v-if="bookList.length > 0">
            <!-- title： 设置鼠标的悬浮提示 -->
            <!-- 格式：
            <元素 v-for="（循环变量，循环下标） in 可循环的目标"></元素> -->

            <!-- book <=== bookList[idx] -->
            <div class="book" :title="book.name" v-for="(book, idx) in bookList">
                <a href="">
                    <div class="figure">
                        <!-- img标签的alt属性有什么用？ 图片失败的提示文字  -->
                        <img v-bind:src="book.picSrc" v-bind:alt="book.name">
                    </div>
                    <h4 class="title" :title="book.name">{{book.name}}</h4>
                    <p class="price"><span class="num">{{book.price | guolv}}</span></p>
                </a>
                <button class='btn' @click="shanchu(idx)" title="移出购物车">-</button>
            </div>
        </div>
        <div v-else>没有商品内容了。。嘤嘤嘤,赶紧去买把<a></a></div>
        <div>总价:{{zongjia}}</div>
    </div>
    <!-- 引入VUE核心js文件 -->
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.js"></script>

    <script>
        // 目标：用vue来实现模板渲染
        var bookList = [
            {
                name: '当你学会独处',
                price: 29.9,
                picSrc: 'http://img3m8.ddimg.cn/70/15/28522168-1_l_3.jpg'
            },
            {
                name: 'JavaScript高级程序设计',
                price: 78,
                picSrc: 'http://img3m3.ddimg.cn/2/21/22628333-1_b_2.jpg'
            },
            {
                name: '断舍离',
                price: 29.9,
                picSrc: 'http://img3m3.ddimg.cn/68/20/23271503-1_l_10.jpg'
            },
            {
                name: '王子与贫儿',
                price: 39.9,
                picSrc: 'http://img3m0.ddimg.cn/67/20/28535530-1_l_3.jpg'
            },
            {
                name: '高效能人士的七个习惯',
                price: 59.3,
                picSrc: 'http://img3m2.ddimg.cn/3/4/25253022-1_b_11.jpg'
            }
        ]
        new Vue({
            el: '#app',
            data: {
                bookList: bookList,
            },
            filters: {
                guolv(val) {
                    return '$' + val;
                }
            },
            methods: {
                //删除功能
                shanchu(idx) {
                    if (!window.confirm('确定要删除吗')) {
                        return
                    }
                    this.bookList.splice(idx, 1)
                }
            },
            computed: {
                //计算总价功能
                zongjia() {
                    let num = 0;
                    this.bookList.forEach(item => {
                        num += item.price
                    });
                    return num.toFixed(2)
                }
            }
        })

    </script>
</body>

</html>